<?php
namespace Entities\Corp;
use Doctrine\ORM\Query;

use Doctrine\ORM\EntityRepository;

class PessoaFisicaRepository extends EntityRepository
{
	public function listar($params = array(), $notIn = array())
	{
		$qb		= $this->getEntityManager()->createQueryBuilder();
		$qb->select('p')
		->from('Entities\Corp\PessoaFisica', 'p');

		$where	= $qb->expr()->andX();
		$where->add($qb->expr()->eq(1,1));
		foreach ($params as $key => $value)
		{
			$where->add($qb->expr()->eq($key, $value));
		}
		foreach ($notIn as $key => $value)
		{
			$where->add($qb->expr()->notIn($key, $value));
		}
		$qb->where($where)
		->orderBy('p.ds_nome');

		return $this->getEntityManager()->createQuery($qb->getDQL());
	}

	public function listCombo($params = array(), $notIn = array())
	{
		$return	= array();
		$lista	= $this->listar($params, $notIn);
		foreach ($lista->getResult() as $value)
		{
			$return[$value->getIdPessoa()]	= $value->getDsNome();
		}
		return $return;
	}

	public function getLogin($login, $senha)
	{
		$login	= strip_tags($login);
		$login	= str_replace(array('"',"'",'>','<',' '), '', $login);
		$cpf	= preg_replace( '/[^0-9]/', '', $login );

		$dql = "SELECT u FROM \Entities\Corp\PessoaFisica u
                LEFT JOIN u.emails e
                WHERE (u.nu_cpf = '{$cpf}' OR e.ds_email = '{$login}')
				AND u.senha = '{$senha}'
				AND u.fl_status	= 1
				AND u.fl_usuario = 1";
		$query = $this->getEntityManager()->createQuery($dql);
		$retorno	= $query->getResult(Query::HYDRATE_OBJECT);

		if (count($retorno))
			return $retorno[0];
	}
}